﻿using System;
using System.Collections.Generic;
using System.IO;
using GemBox.Spreadsheet;
using System.Xml.Serialization;

namespace cndp.Model
{
    /// <summary>
    /// JumpAppendix - Denna klass läser in dem olika hopp som existerar från en excelfil och lagrar det i en lista
    /// </summary>
    [Serializable]
    public class JumpAppendix
    {
        #region List

        [XmlElement(typeof(Jump))]
        public List<Jump> JumpAppendixList = new List<Jump>();

        #endregion

        #region Methods

        public void ReadAllJumpsFromAppendixFile(string fileName)
        {
            try
            {
                SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
                var workbook = ExcelFile.Load(fileName);
                var worksheet = workbook.Worksheets.ActiveWorksheet;
                for (var i = 3; i <= 123; i++)
                {
                    var tempList = new List<string>();
                    for (var a = 'A'; a <= 'V'; a++)
                    {
                        var tempString = new string(a, 1);
                        tempList.Add(worksheet.Cells[tempString + i].GetFormattedValue());
                    }

                    var tempJump = new Jump(Convert.ToInt32(tempList[0]), tempList[1], Convert.ToDouble(tempList[2]),
                        Convert.ToDouble(tempList[3]),
                        Convert.ToDouble(tempList[4]), Convert.ToDouble(tempList[5]),
                        Convert.ToDouble(tempList[6]), Convert.ToDouble(tempList[7]),
                        Convert.ToDouble(tempList[8]), Convert.ToDouble(tempList[9]),
                        Convert.ToDouble(tempList[10]), Convert.ToDouble(tempList[11]),
                        Convert.ToDouble(tempList[12]), Convert.ToDouble(tempList[13]),
                        Convert.ToDouble(tempList[14]), Convert.ToDouble(tempList[15]),
                        Convert.ToDouble(tempList[16]), Convert.ToDouble(tempList[17]),
                        Convert.ToDouble(tempList[18]), Convert.ToDouble(tempList[19]),
                        Convert.ToDouble(tempList[20]), Convert.ToDouble(tempList[21]));
                    JumpAppendixList.Add(tempJump);
                }
            }

            catch (IOException e)
            {
                Console.WriteLine(@"Error reading from {0}. Message = {1}", fileName, e.Message);
            }
        }

        // A = Straight
        // B = Pike
        // C = Tuck
        // D = Free

        public double FindDifficulty(int jumpId, string style, double height)
        {
            var jumpFound = JumpAppendixList.Find(
                delegate(Jump j) { return j.JumpId == jumpId; }
                );

            if (jumpFound == null) throw new Exception("\nJumpAppendix.FindDifficulty - Jump not found");

            var difference = Math.Abs(height*0.00001); //Tolererad skillnad vid jämförelse av två doubles.

            #region height1

            if (Math.Abs(height - 1.0) <= difference)
            {
                if (String.Compare(style, "Straight", StringComparison.OrdinalIgnoreCase) == 0)
                    //Spelar ej roll med stor bokstav eller liten
                {
                    return jumpFound.OneMeter.Straight;
                }

                if (String.Compare(style, "Pike", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.OneMeter.Pike;
                }

                if (String.Compare(style, "Tuck", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.OneMeter.Tuck;
                }

                if (String.Compare(style, "Free", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.OneMeter.Free;
                }
            }
                #endregion

                #region height3

            else if (Math.Abs(height - 3.0) <= difference)
            {
                if (String.Compare(style, "Straight", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.ThreeMeter.Straight;
                }

                if (String.Compare(style, "Pike", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.ThreeMeter.Pike;
                }

                if (String.Compare(style, "Tuck", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.ThreeMeter.Tuck;
                }

                if (String.Compare(style, "Free", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.ThreeMeter.Free;
                }
            }
                #endregion

                #region height5

            else if (Math.Abs(height - 5.0) <= difference)
            {
                if (String.Compare(style, "Straight", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.FiveMeter.Straight;
                }

                if (String.Compare(style, "Pike", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.FiveMeter.Pike;
                }

                if (String.Compare(style, "Tuck", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.FiveMeter.Tuck;
                }

                if (String.Compare(style, "Free", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.FiveMeter.Free;
                }
            }

                #endregion

                #region height7.5

            else if (Math.Abs(height - 7.5) <= difference)
            {
                if (String.Compare(style, "Straight", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.SevenMeter.Straight;
                }

                if (String.Compare(style, "Pike", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.SevenMeter.Pike;
                }

                if (String.Compare(style, "Tuck", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.SevenMeter.Tuck;
                }

                if (String.Compare(style, "Free", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.SevenMeter.Free;
                }
            }

                #endregion

                #region height10

            else //if (Math.Abs(height - 10) <= difference)
            {
                if (String.Compare(style, "Straight", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.TenMeter.Straight;
                }

                if (String.Compare(style, "Pike", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return jumpFound.TenMeter.Pike;
                }

                if (style.Equals("Tuck"))
                {
                    return jumpFound.TenMeter.Tuck;
                }

                if (style.Equals("Free"))
                {
                    return jumpFound.TenMeter.Free;
                }
            }

            #endregion

            throw new Exception("\nJumpAppendix.FindDifficulty - Wrong height input");
        }

        public string FindJumpName(int jumpId)
        {
            var jumpFound = JumpAppendixList.Find(
                delegate(Jump j) { return j.JumpId == jumpId; }
                );

            if (jumpFound != null)
            {
                return jumpFound.JumpName;
            }
            throw new Exception("\nJumpAppendix.FindDifficulty - Jump not found");
        }

        #endregion
    }
}